package com.mask.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVodTemplateRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.code.SystemEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.exception.RollException;
import com.mask.roll.common.util.RollResponse;
import com.mask.vod.service.VodService;
import com.mask.vod.util.ConstantVodUtils;
import com.mask.vod.util.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author hx
 */
@RestController
@RequestMapping("/vod/video")
@CrossOrigin
@Api(tags = "视频管理")
@Slf4j
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     * 上传视频到阿里云
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("上传视频")
    public RollResponse uploadVideo(MultipartFile file) {
        //返回上传视频id
        String videoId = vodService.uploadVideo(file);
        return RollResponse.result(ServiceEnums.VIDEO_UPLOAD_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.VIDEO_UPLOAD_SUCCESS.getMessage(),videoId);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除视频")
    public RollResponse deleteVod(@PathVariable("id") String id){
        vodService.deleteVideo(id);
        return RollResponse.result(ServiceEnums.VOD_DELETE_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.VOD_DELETE_SUCCESS.getMessage());
    }

    @DeleteMapping("/delete/match")
    @ApiOperation(value = "删除多个视频")
    public RollResponse deleteVodBatch(@ApiParam(name = "listId",value = "视频id",required = true)
                                  @RequestParam("listId")List<String> listId){
        vodService.deleteVideoMatch(listId);
        return RollResponse.result(ServiceEnums.VOD_DELETE_MATCH_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.VOD_DELETE_MATCH_SUCCESS.getMessage());
    }

    /**
     * 根据视频id获取视频凭证
     * @param id
     * @return
     */
    @GetMapping("get/auth/{id}")
    @ApiOperation("获取视频凭证")
    public RollResponse getPlayAuth(@ApiParam("视频id") @PathVariable String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client =
                    InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            log.info("{}获取视频凭证成功:{}",id,playAuth);
            return RollResponse.result(ServiceEnums.VOD_VIDEO_GET_AUTH_SUCCESS.getCode(),
                    RollConstants.SUCCESS,ServiceEnums.VOD_VIDEO_GET_AUTH_SUCCESS.getMessage(),playAuth);
        }catch(Exception e) {
            log.error("获取视频凭证异常:{}",e.getMessage());
            throw new RollException(SystemEnums.VOD_AUTH_ERROR.getCode(),
                    RollConstants.ERROR,SystemEnums.VOD_AUTH_ERROR.getMessage());
        }
    }
}
